<template>
  <div class="mod-job__schedule">
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="bean名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
      <!-- <el-form-item>
        <el-button v-if="state.hasPermission('sys:schedule:delete')" type="danger" @click="state.deleteHandle()">删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:schedule:pause')" type="danger" @click="pauseHandle()">暂停</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:schedule:resume')" type="danger" @click="resumeHandle()">恢复</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:schedule:run')" type="danger" @click="runHandle()">执行</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:schedule:log')" type="success" @click="logHandle()">日志列表</el-button>
      </el-form-item> -->
    </el-form>
    <el-table v-loading="resultData.dataListLoading" :data="resultData.dataList" border style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="name" label="变量名" header-align="center" align="center"></el-table-column>
      <el-table-column prop="parameter" label="变量值" header-align="center" align="center"></el-table-column>
      <el-table-column prop="createDate" label="创建时间" show-overflow-tooltip header-align="center" align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="248">
        <template v-slot="scope">
          <el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="primary" link @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @current-change="handleCurrentChange" v-model:current-page="resultData.page" :page-size="resultData.limit" :size="resultData.total" layout="total, prev, pager, next" :total="resultData.total" />
    <!-- <el-pagination :current-page="resultData.page" :page-sizes="[10, 20, 50, 100]" :page-size="resultData.limit" :total="resultData.total" layout="total, sizes, prev, pager, next, jumper"> </el-pagination> -->
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
    <!-- 弹窗, 日志列表 -->
    <!-- <log ref="logRef"></log> -->
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, toRefs } from "vue";
import baseService from "@/service/baseService";
import { useRouter } from "vue-router";
import AddOrUpdate from "./function-add-or-update.vue";
import { ElMessage, ElMessageBox } from "element-plus";
const router = useRouter();
const resultData = reactive({
  page: ref(1),
  limit: ref(10),
  dataList: [],
  total: ref(1),
  dataListLoading: ref(false)
});
const handleCurrentChange = (val: number) => {
  resultData.page = val;
  getDataList();
};
const getDataList = () => {
  baseService
    .get("function/list", {
      page: resultData.page,
      limit: resultData.limit
    })
    .then((res) => {
      resultData.dataListLoading = false;
      resultData.dataList = res.data.records;
      resultData.total = Number(res.data.total);
    })
    .catch(() => {
      resultData.dataListLoading = false;
    });
};
const dataForm = ref({
  name: ""
});
const deleteHandle = (id: string) => {
  ElMessageBox.confirm("确定进行[删除]操作?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      baseService.delete(`function/remove/${id}`, {}).then((res) => {
        ElMessage.success({
          message: "成功",
          duration: 500,
          onClose: () => {
            getDataList();
          }
        });
      });
    })
    .catch(() => {
      //
    });
};

const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id);
};
onMounted(() => {
  getDataList();
});
</script>
